package org.campuscontent.util;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JEditorPane;
import org.campuscontent.config.Config;

public class MovableEditor extends JEditorPane implements FocusListener
{
        private int actionIndex = 0;
        int i = 0;
        
        public String getNetString()
        {
                int index;
                String content = this.getText();
        content = transform(content);
        while((index=content.indexOf("\r"))!=-1){
                if(index + 2 < content.length())
                        content = content.substring(0, index) + content.substring(index+2, content.length()-1);
                else
                        content = content.substring(0, index);
        }
                return content;
        }
        
        
        
        // string:  0....colorFinished...begin...colorNow...end....NO Change...end
        public void setColor(int begin, int end, String colorFinished, String colorNow)
        {
//              actionIndex = begin;
                String inputString = this.getNetString();
        String newText =
            "<table border=0 cellpadding=0><tr><td bgcolor=";
          newText += colorFinished + ">";
          newText += inputString.substring(0, begin);
          newText += "</td>";
          newText += "<td bgcolor=" + colorNow + ">";
          newText += inputString.substring(begin, end) + "</td>";
          newText += "<td>" + inputString.substring(end) +
            "</td></tr></table>";
          this.setText(newText);
        }
        
        // string:  0....colorFinished...index...colorWrong...end
        public void moveToEnd(int index, String colorFinished, String colorWrong)
        {
                actionIndex = index;
                String inputString = this.getNetString();
                String newText =
            "<table border=0 cellpadding=0><tr><td bgcolor=";
          newText += colorFinished + ">";
          newText += inputString.substring(0, index);
          newText += "</td>";
          newText += "<td bgcolor=" +
            colorWrong +
            ">";
          newText += inputString.substring(index) +
            "</td></tr></table>";
          this.setText(newText);
        }
        
        public void moveToBegin()
        {
                actionIndex = 0;
                setColor(0,0,"","");
        }

        public void back()
        {
                if (actionIndex>1)
                {
                        actionIndex--;
                        setColor(actionIndex-1,actionIndex,Config.ColorFinished,"");
                }
                else
                {
                        moveToBegin();
                }

        }
        
        public void back(int startPnt)
        {
                        setColor(startPnt,getNetString().length(),Config.ColorFinished,Config.ColorNormal);
        }
        
        public void forward()
        {
                if (actionIndex<getNetString().length())
                {
                        setColor(actionIndex,actionIndex+1 ,Config.ColorFinished,Config.ColorNow);
                        actionIndex++;
                }
        }
        
        public void forward(int startPnt, int endPnt)
        {
                setColor(startPnt,endPnt ,Config.ColorFinished,Config.ColorNow);
        }
        
        public int getActionIndex()
        {
                return actionIndex;
        }
        public String transform(String content) {
                int begin = content.lastIndexOf("<");
                int end = content.lastIndexOf(">");
                while (begin != -1) {
                        content = content.substring(0, begin)
                                        + content.substring(end + 1, content.length());
                        begin = content.lastIndexOf("<");
                        end = content.lastIndexOf(">");
                }
                content = content.replaceAll("\n", "");
                content = content.replaceAll(" ", "");
                return content;
        }

        public MovableEditor()
        {
                super();
                this.addFocusListener(this);
        }

        public void focusGained(FocusEvent arg0)
        {
                this.setColor(0, 0, Config.ColorNormal, Config.ColorNormal);
        }

        public void focusLost(FocusEvent arg0)
        {
                // TODO Auto-generated method stub
                
        }
}
